/*
 * Garden.h
 *
 *  Created on: 05.11.2010
 *      Author: 123
 */

#ifndef _GARDEN_H_
#define _GARDEN_H_
#include <oslib/oslib.h>
#include "struct.h"

#include "Colors.h"
#include "Utility.h"
extern "C" {

#include    <lauxlib.h>
#include    <lualib.h>
#include "pgeFont.h"
#include<time.h>
#include <stdio.h>
}
extern char *LoadingInfo;
extern pgeFont* nash;

enum pos {
	right = 1, up, down, left
};
class Garden {
	int LineX;
	int LineY;

	bool HeroCatchAnAnimal;
	int ChangeAnimalNumber;
	int i;
	int j;
	animals animal[100];
	int AnimalAmount;
	int CageXY[4][2];
	int TileRand[16][17];

	void MoveAndEatAnimals(animals *a);
public:
	SAnimalInfo animalinfo[100];
	//OSL_IMAGE
	OSL_IMAGE *AnimalTex[100];
	OSL_IMAGE* TileTex[2];
	void RandTile() {
		for (i = 0; i < 16; i++) {
			for (j = 0; j < 17; j++) {
				//	srand((unsigned)time(0));
				TileRand[i][j] = rand() % 2;
			}
		}
	}
	int Load(const char *name) {

		char line[1024];
		char* str = line;

		FILE * fd = fopen(name, "r");
		if (!fd)
			fprintf(stderr, "fd load fail");
		int i = 0;
		while (1) {

			if (!fgets(line, 1024, fd) || line[0] == '/')
				break;
			sscanf(line, "%f", &animalinfo[i].speed);

			if (!fgets(line, 1024, fd))
				break;
			sscanf(line, "%d", &animalinfo[i].xeat);

			if (!fgets(line, 1024, fd))
				break;
			sscanf(line, "%d", &animalinfo[i].yeat);
			if (!fgets(line, 1024, fd))
				break;
			sscanf(line, "%d", &animalinfo[i].maxLevelAmount);
			if (!fgets(line, 1024, fd))
				break;
			sscanf(line, "%d", &animalinfo[i].nextLevelAmount[0]);
			if (!fgets(line, 1024, fd))
				break;
			sscanf(line, "%d", &animalinfo[i].nextLevelAmount[1]);

		}
		while (1) {
			if (!fgets(line, 1024, fd) || line[0] == '*')
				break;
			sscanf(line, "%f", &animal[AnimalAmount].x);
			if (!fgets(line, 1024, fd))
				break;

			sscanf(line, "%f", &animal[AnimalAmount].y);
			if (!fgets(line, 1024, fd))
				break;
			sscanf(line, "%d", &animal[AnimalAmount].strength);
			if (!fgets(line, 1024, fd))
				break;
			sscanf(line, "%d", &animal[AnimalAmount].type);
			if (!fgets(line, 1024, fd))
				break;
			sscanf(line, "%d", &animal[AnimalAmount].NumberCage);
			animal[AnimalAmount].NumberCage -= 1;
			AnimalAmount++;

		}
		i = 0;
		while (1) {
			char TexName[32] = "data/";

			if (!fgets(line, 1024, fd))
				break;

			str = line;
			sscanf(str, "%s", TexName + 5);
			sprintf(LoadingInfo, "%d,%s", i + 1, TexName);
			AnimalTex[i] = oslLoadImageFile(TexName, OSL_IN_RAM, OSL_PF_8888);
			if (!AnimalTex[i]) {
				fprintf(stderr, "fail 2");
				return 0;
			}
			AnimalTex[i]->centerX = AnimalTex[i]->stretchX;
			AnimalTex[i]->centerY = AnimalTex[i]->stretchY;
			i++;
		}
		fclose(fd);
		return 1;
	}
	;

	void AddAnimal(int x, int y, int strength, int type, int NumberCage) {
		animal[AnimalAmount].x = x;
		animal[AnimalAmount].y = y;
		animal[AnimalAmount].strength = strength;
		animal[AnimalAmount].type = type;
		animal[AnimalAmount].NumberCage = NumberCage - 1;
		this->AnimalAmount++;
	}
	;
	Garden() {
		HeroCatchAnAnimal = 0;
		i = 0;
		j = 0;
		ChangeAnimalNumber = 0;
		AnimalAmount = 0;
		LineX = 0;
		LineY = 0;
		CageXY[0][0] = 0;
		CageXY[0][1] = 0;

		CageXY[1][0] = 160;
		CageXY[1][1] = 272;

		CageXY[2][0] = 320;
		CageXY[2][1] = 272;

		CageXY[3][0] = 480;
		CageXY[3][1] = 272;
	}
	void Think();
	void Control();
	void Draw();
};

extern Garden AnimalGarden;
#endif /* GARDEN_H_ */
